
package com.fl.util.compress;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;

/**
 * tar文件格式压缩实现 调用Aapache Commons Compress包中的
 * 
 * <pre>
 * TarArchiveInputStream 
 * TarArchiveOutputStream
 * </pre>
 * 
 * 实现<br>
 * 
 * 
 */
public class TarCompressor extends AbstractFileCompressor {

    /**
     * 构造函数 <br>
     * */
    public TarCompressor() {
        super();
    }

    /**
     * 使用tar格式压缩文件<br>
     * 
     * @see com.unionpay.common.util.compress.FileCompressor#compress(java.io.File, java.io.File)
     */
    @Override
    public void compress(File srcFile, File destFile) throws IOException {
        TarArchiveOutputStream out = null;
        InputStream is = null;
        try {
            is = new BufferedInputStream(new FileInputStream(srcFile), buffSize);
            out = new TarArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), buffSize));
            TarArchiveEntry entry = new TarArchiveEntry(srcFile.getName());
            entry.setSize(srcFile.length());
            out.putArchiveEntry(entry);
            copyStreams(is, out);
            out.closeArchiveEntry();
        } finally {
            closeStreams(is);
            closeStreams(out);
        }
    }

    /**
     * 使用tar格式解压文件<br>
     * 
     * @see com.unionpay.common.util.compress.FileCompressor#decompress(java.io.File, java.io.File)
     */
    @Override
    public void decompress(File srcFile, File destDir) throws IOException {
        TarArchiveInputStream is = null;
        try {
            is = new TarArchiveInputStream(new BufferedInputStream(new FileInputStream(srcFile), buffSize));
            TarArchiveEntry entry = null;
            while ((entry = is.getNextTarEntry()) != null) {
                if (entry.isDirectory()) {
                    File directory = new File(destDir, entry.getName());
                    directory.mkdirs();
                } else {
                    BufferedOutputStream bos = null;
                    try {
                        byte[] buffer = new byte[buffSize];
                        bos = new BufferedOutputStream(new FileOutputStream(new File(destDir, entry.getName())),
                                buffSize);
                        int len;
                        long size = entry.getSize();
                        while (size > 0) {
                            if (size < buffSize) {
                                len = is.read(buffer, 0, (int) size);
                                size -= len;
                            } else {
                                len = is.read(buffer);
                                size -= len;
                            }
                            bos.write(buffer, 0, len);
                        }
                    } finally {
                        closeStreams(bos);
                    }
                }
            }
        } finally {
            closeStreams(is);
        }
    }
}
